package com.jams.goodox.activity.serve.park;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.NumberPicker;

import com.jams.goodox.R;
import com.jams.goodox.utils.DayUtils;
import com.jams.goodox.widget.CustomNumberPicker;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import java.lang.reflect.Field;

/**
 * 发布行程时间选择框
 * Created by jams_zhen on 2017/5/31.
 */

public class ParkTimeDialog extends DialogFragment {

    @Bind(R.id.number_hour)
    CustomNumberPicker numberHour;
    private OnChooseDate mListener;

    private int positions;
    private long curDateLong;
    private String[] lists;

    public static ParkTimeDialog getParkTimeDialog(int position,long curDateLong){
        ParkTimeDialog numberPickerDialog=new ParkTimeDialog();
        Bundle bundle=new Bundle();
        bundle.putInt("posi",position);
        bundle.putLong("curDateLong",curDateLong);
        numberPickerDialog.setArguments(bundle);
        return numberPickerDialog;
    }

    @Override
    public void onAttach(Context context) {
        if (context instanceof OnChooseDate) {
            mListener = (OnChooseDate) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement ABC_Listener");
        }
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        positions = getArguments().getInt("posi");
        curDateLong=getArguments().getLong("curDateLong");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
        final Window window = getDialog().getWindow();
        View view = inflater.inflate(R.layout.part_add_time_dialog, container);
        ButterKnife.bind(this, view);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//注意此处
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        window.setGravity(Gravity.BOTTOM);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        lists=DayUtils.getNextMonthArray(curDateLong);
        numberHour.setDisplayedValues(lists);
        numberHour.setMinValue(0);
        numberHour.setMaxValue(lists.length - 1);
        numberHour.setValue(positions);
        numberHour.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        setNumberPickerDividerColor(numberHour);
        numberHour.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker numberPicker, int i, int i1) {
                positions=i1;
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.img_close, R.id.tx_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_close:
                getDialog().cancel();
                break;
            case R.id.tx_sure:
                if(positions>=lists.length)return;
                mListener.onChooseDate(lists[positions],positions);
                dismiss();
                break;
        }
    }

    public interface OnChooseDate {
        void onChooseDate(String showDate,int position);
    }

    /**
     * 设置数字选择器分割线高度&颜色
     *
     * @param numberPicker
     */
    private void setNumberPickerDividerColor(NumberPicker numberPicker) {
        NumberPicker picker = numberPicker;
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                pf.setAccessible(true);
                try {
                    //设置分割线的颜色值
                    pf.set(picker, new ColorDrawable(this.getResources().getColor(R.color.divider_line_d3)));
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (Resources.NotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
        // 分割线高度
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDividerHeight")) {
                pf.setAccessible(true);
                try {
                    int result = 2;
                    pf.set(picker, result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }
}
